package com.ningxia.water.mapper;

import com.ningxia.water.entity.TsWellWarn;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ningxia.water.vo.out.WarnInfoVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author hz
 * @since 2021-03-15
 */
public interface TsWellWarnMapper extends BaseMapper<TsWellWarn> {


    /**
     * 通过status获取报警数量(通过status来判断此报警是否已处理)
     * @param warn_code
     * @return
     */
    @Select("select count(1) from ts_well_warn where warn_code = #{warn_code} and status= #{status}")
    Integer findByWarnCode(Integer warn_code,Integer status);

    /**
     * 获取报警数量（未处理，已处理）
     * @param status
     * @return
     */
    @Select("select count(1) from ts_well_warn where status = #{status}")
    Integer getWarnCountsByStatus(Integer status);

    /**
     * 更新报警记录状态
     * @param id
     * @param status
     * @return
     */
    @Update("update ts_well_warn set status = #{status} where id = #{id} ")
    Integer updateByWarnId(Integer id,Integer status);

    /**
     * 查询此机井下是否有未处理的报警记录
     * @param wellId
     * @return
     */
    @Select("select count(1) from ts_well_warn where well_id = #{wellId} and status = 0")
    Integer getWarnCountsByWellId(Integer wellId);

    /**
     * 查询全部报警记录
     * @return
     */
    @Select("SELECT\n" +
            "ww.id AS warnId,\n" +
            "wi.well_name,\n" +
            "ai.applicant_name,\n" +
            "ww.warn_code AS warnType,\n" +
            "wi.zone_id,\n" +
            "wi.zone_type,\n" +
            "ww.STATUS AS STATUS,\n" +
            "ww.warn_time AS warnTime\n" +
            "FROM\n" +
            "ts_well_warn ww\n" +
            "LEFT JOIN ts_well_info wi ON wi.well_id = ww.well_id\n" +
            "LEFT JOIN ts_applicant_well aw ON aw.well_id = wi.well_id\n" +
            "LEFT JOIN ts_applicant_info ai ON ai.applicant_id = aw.applicant_id")
    List<WarnInfoVo> getWarnInfos();

    /**
     * 通过条件查询报警记录
     * @param applicantName 用户名
     * @param wellName 机井名称
     * @param status 报警记录状态
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    @Select("<script>" +
            "SELECT\n" +
            "wi.well_id AS wellId,\n" +
            "ww.id AS warnId,\n" +
            "wi.well_name,\n" +
            "ai.applicant_name,\n" +
            "ww.warn_code AS warnType,\n" +
            "wi.zone_id,\n" +
            "wi.zone_type,\n" +
            "ww.STATUS AS STATUS,\n" +
            "ww.warn_time AS warnTime,\n" +
            "ai.product_address AS address\n" +
            "FROM\n" +
            "ts_well_warn ww\n" +
            "LEFT JOIN ts_well_info wi ON wi.well_id = ww.well_id\n" +
            "LEFT JOIN ts_applicant_well aw ON aw.well_id = wi.well_id\n" +
            "LEFT JOIN ts_applicant_info ai ON ai.applicant_id = aw.applicant_id" +
            "<where>" +
            "<if test='applicantName!=null'>" +
            "and ai.applicant_name like CONCAT('%',#{applicantName},'%') " +
            "</if>"+
            "<if test='wellName!=null'>" +
            "and wi.well_name like CONCAT('%',#{wellName},'%') " +
            "</if>"+
            "<if test='status!=null'>" +
            "and ww.status=#{status} " +
            "</if>"+
            "<if test='startTime!=null and endTime!=null'>" +
            "and ww.warn_time BETWEEN #{startTime} and #{endTime}" +
            "</if>"+
            "</where>" +
            "</script>")
    List<WarnInfoVo> getWarnInfosByConditions(String applicantName, String wellName, Integer status, String startTime, String endTime);
}
